/*  
 *  ------ [802_11b] - complete example receive  ------
 *  
 *  Explanation: This is a complete example for XBee 802.15.4.
 *  This example shows how to receive a packet. When the source 
 *  address is the expected one, an answer is sent back to the
 *  emitter Waspmote
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspXBee802.h>
#include <WaspFrame.h>

// pointer to the packet to send
packetXBee* packet;
long previous=0;
uint8_t destination[8];


void setup()
{
  // init USB port
  USB.ON();
  USB.println(F("802_11b example"));

  // init XBee 
  xbee802.ON(); 

}


void loop()
{
  // Wait message for 20 seconds
  previous=millis();
  while( (millis()-previous) < 20000 )
  {
    // check available data in RX buffer
    if( xbee802.available() > 0 ) 
    {
      // parse information
      xbee802.treatData(); 

      // check RX flag after 'treatData'
      if( !xbee802.error_RX ) 
      {
        // check available packets
        while( xbee802.pos>0 )
        {

          /*** Available info in 'xbee802.packet_finished' structure ***/

          // Get Source's MAC address
          destination[0]=xbee802.packet_finished[xbee802.pos-1]->macSH[0]; 
          destination[1]=xbee802.packet_finished[xbee802.pos-1]->macSH[1]; 
          destination[2]=xbee802.packet_finished[xbee802.pos-1]->macSH[2]; 
          destination[3]=xbee802.packet_finished[xbee802.pos-1]->macSH[3]; 
          destination[4]=xbee802.packet_finished[xbee802.pos-1]->macSL[0]; 
          destination[5]=xbee802.packet_finished[xbee802.pos-1]->macSL[1]; 
          destination[6]=xbee802.packet_finished[xbee802.pos-1]->macSL[2]; 
          destination[7]=xbee802.packet_finished[xbee802.pos-1]->macSL[3]; 


          /************ 1.CREATE FRAME  ***************/

          // 1.1. Create new ASCII frame
          frame.createFrame(ASCII, "WASPMOTE_11b");  

          // 1.2. add frame fields
          frame.addSensor(SENSOR_STR, "Response_message");


          /************  2.SEND PACKET  ***************/

          // 2.1. Send response    
          packet=(packetXBee*) calloc(1,sizeof(packetXBee)); 
          packet->mode=UNICAST;                  

          // 2.2. sets Destination parameters
          xbee802.setDestinationParams(packet, destination, frame.buffer, frame.length, MAC_TYPE);

          // 2.3. send packet
          xbee802.sendXBee(packet);

          USB.print(F("New packet received. send response: "));
          
          // 2.4. Check TX flag
          if( xbee802.error_TX == 0) 
          {
            USB.println(F("ok"));
          }
          else 
          {
            USB.println(F("error")); 
          }

          // 2.5. free memory
          free(packet);
          packet=NULL; 

          // 2.6. Free variable 
          free(xbee802.packet_finished[xbee802.pos-1]);
          xbee802.packet_finished[xbee802.pos-1]=NULL;

          // 2.7. Decrement packet counter
          xbee802.pos--; 
        } 
      }
    }
  }/* end while */

  delay(5000);
}





